package com.exam.util;

import java.io.File;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.postgresql.jdbc3.Jdbc3PoolingDataSource;

import zuojie.esql.Esql;
import zuojie.esql.EsqlPgsqlImpl;

import com.exam.UserException;
import com.exam.entity.Marker;
import com.exam.util.loader.ExcelLoader;
import com.exam.util.loader.Loader;
import com.exam.util.loader.LoaderHandler;

/**
 *
 * @author FangZhiyang
 */
public class 批量导入Marker
{

	private static final String TITLE_IDENTIFY = "病案ID";
	private static final String TITLE_ADDRESS = "现地址详细";
	private static final String TITLE_DATE = "本次确诊日期";
	private static final String TITLE_FLAG = "标识";
	
	private static final DateFormat DF = new SimpleDateFormat("yyyy-MM");
	
	private String filename;
	private int importCount = 0;
	private boolean success;
	private List<String> messages;

	public void execute(Esql esql) throws Exception
	{
		File xls = new File("c:/all1.xls");
		
		Loader loader = new ExcelLoader(new MarkerLoadHandler(), xls, esql);
		try
		{
			loader.load();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		if (loader.getFailCount() > 0)
		{
			success = false;
			messages = loader.getMessages();
		}
		else
		{
			success = true;
		}
	}

	public String getFilename()
	{
		return filename;
	}

	public void setFilename(String filename)
	{
		this.filename = filename;
	}

	public int getImportCount()
	{
		return importCount;
	}

	public void setImportCount(int importCount)
	{
		this.importCount = importCount;
	}

	public boolean isSuccess()
	{
		return success;
	}

	public void setSuccess(boolean success)
	{
		this.success = success;
	}

	public List<String> getMessages()
	{
		return messages;
	}

	public void setMessages(List<String> messages)
	{
		this.messages = messages;
	}

	//===================================
	
	private class MarkerLoadHandler implements LoaderHandler
	{
		public void load(Map<String, String> data, Esql esql) throws Exception
		{
			Marker marker = new Marker();
			
			//病案ID
			String identify = data.get(TITLE_IDENTIFY);
			if (null == identify || "".equals(identify))
			{
				throw new UserException(TITLE_IDENTIFY + "为必填项，不能为空！");
			}
			
			//现地址详细
			String address = data.get(TITLE_ADDRESS);
			if (null == address || "".equals(address))
			{
				throw new UserException(TITLE_ADDRESS + "为必填项，不能为空！");
			}
			//本次确诊日期
			String date = data.get(TITLE_DATE);
			if (null == date && !"".equals(date))
			{
				throw new UserException(TITLE_DATE + "为必填项，不能为空！");
			}
			
			//标识
			String flag = data.get(TITLE_FLAG);
			if (null == flag || "".equals(flag))
			{
				throw new UserException(TITLE_FLAG + "为必填项，不能为空！");
			}
			
			marker.setIdentify(identify);
			marker.setAddress(address);
			System.out.println("读入" + marker.getAddress());
			marker.setLat(0.0);
			marker.setLng(0.0);
			marker.setDate(dateValidator(date, TITLE_DATE));
			marker.setFlag(flag.equals("1") ? true : false);
			
//			dao.addMarker(marker);
			Marker exist = esql.query(Marker.class, "select * from t_marker where identify = ?", marker.getIdentify());
			if (exist == null)
			{
				esql.helper().insert(marker, "t_marker", null, "id");
				System.out.println((importCount+1) + "成功导入" + marker.getAddress());
			}
			else
			{
				System.out.println(exist.getAddress());
			}
			importCount++;
		}
	}
	
	/** 日期类型验证器 */
	private Date dateValidator(String validateStr, String category) throws Exception
	{
		Date date;
		try
		{
			date = DF.parse(validateStr);
		} 
		catch (Exception e)
		{
			throw new UserException(String.format(category + " %s 日期格式不符合", validateStr));
		}
		return date;
	}
	
	public static void main(String[] args) throws Exception
	{
		Jdbc3PoolingDataSource ds = new Jdbc3PoolingDataSource();
		ds.setServerName("localhost");
		ds.setDatabaseName("disease");
		ds.setUser("peuser");
		ds.setPassword("123");
		
		// 根据数据库类型和数据源创建esql对象
		Esql esql = new EsqlPgsqlImpl();
		esql.setDataSource(ds);
		
		try
		{
			esql.begin(Connection.TRANSACTION_READ_COMMITTED);
			
			批量导入Marker test = new 批量导入Marker();
			test.execute(esql);
			
			esql.commit();
		}
		finally
		{
			esql.end();
		}
	}
}